<template>
  <div class="page">
    <!--  -->
    <el-form :inline="true">
      <el-form-item label="关键字">
        <el-input v-model="query.name" placeholder="请输入关键字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleCurrentChange(1)"
          >查询</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="edit({ id: 0 })">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->

    <el-table :data="list" border>
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.image"
            style="width: 100px"
            mode="scaleToFill"
          />
        </template>
      </el-table-column>
      <el-table-column label="logo">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.logo"
            style="width: 100px"
            mode="scaleToFill"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row, scope.$index)"
          ></el-switch>
          <span>{{ scope.row.status == 1 ? "停用" : "启用" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="子分类/主分类" prop="createTime">
        <template slot-scope="scope">
          <span>{{ scope.row.grade == 1 ? "主分类" : "子分类" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="query.pageNum"
      :page-size="query.pageSize"
      layout="total, prev, pager, next"
      :total="total"
    ></el-pagination>

    <!-- 修改器 -->
    <Add :editRow="editRow" ref="addbox" @updateSuccess="handleEdit"></Add>
  </div>
</template>
<script>
import { brandList, brandEdit } from "@/api/ds/brand";
import Add from "./add.vue";
export default {
  components: {
    Add,
  },
  props: {},
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10,
        name: "",
      },
      list: [],
      total: 0,
      editRow: null,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getData();
  },
  methods: {
    handleEdit() {
      this.query.pageNum = 1;
      this.getData();
    },

    handleCurrentChange(page) {
      this.query.pageNum = page;
      this.getData();
    },
    async getData() {
      let res = await brandList(this.query);
      if (res.code === 200) {
        this.list = res.rows;
        this.total = res.total;
      }
    },

    async handleStatusChange(row, index) {
      let res = await brandEdit({
        id: row.id,
        status: row.status === 1 ? 0 : 1,
      });

      if (res.code === 200) {
        this.list[index].status = this.list[index].status === 1 ? 0 : 1;
      }
    },

    edit(row) {
      this.editRow = row;
      setTimeout(() => {
        this.$refs.addbox.showDialog();
      }, 500);
    },
  },
  mounted() {},
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
  deactivated() {},
};
</script>
<style lang="scss" scoped>
.page {
  padding: 20px;
}
</style>
